//
//  UIView+SLKit.m
//  SLKit
//
//  Created by S_LINE on 2021/10/1.
//  Copyright © 2021 S_LINE. All rights reserved.
//

#import "UIView+SLKit.h"

#import <objc/runtime.h>

static char gNbCommonActionHandlerTapGestureKey;
static char gNbCommonActionHandlerTapBlockKey;
static char gNbCommonClickTimeKey;
static char gNbCommonActionHandlerDoubleTapGestureKey;
static char gNbCommonActionHandlerDoubleTapBlockKey;
static char gNbDoubleClickClickTimeKey;
static char gNbCommonActionHandlerLongPressGestureKey;
static char gNbCommonActionHandlerLongPressBlockKey;
static char gNbLongPressClickTimeKey;

@implementation UIView (SLKit)

- (void)addTapActionWithBlock:(void(^)(void))block {
    // 运行时获取单击对象
    UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &gNbCommonActionHandlerTapGestureKey);
    if (!gesture) {
        // 如果没有该对象，就创建一个
        gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
        [self addGestureRecognizer:gesture];
        // 绑定一下gesture
        objc_setAssociatedObject(self, &gNbCommonActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    // 绑定一下block
    objc_setAssociatedObject(self, &gNbCommonActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
    self.userInteractionEnabled = YES;
}

- (void)handleActionForTapGesture:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        // 取出上面绑定的block
        void(^action)(void) = objc_getAssociatedObject(self, &gNbCommonActionHandlerTapBlockKey);
        if (action) {
            NSDate *date = [NSDate date];
            NSTimeInterval current = [date timeIntervalSince1970] * 1000;
            NSDate *lastClickDate = objc_getAssociatedObject(self, &gNbCommonClickTimeKey);
            NSTimeInterval lastClickTime = [lastClickDate timeIntervalSince1970] * 1000;
            if (current - lastClickTime > 500 || current - lastClickTime < -500) {
                action();
                objc_setAssociatedObject(self, &gNbCommonClickTimeKey, date, OBJC_ASSOCIATION_COPY);
            }
        }
    }
}

- (void)addDoubleTapActionWithBlock:(void(^)(void))block {
    // 运行时获取双击对象
    UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &gNbCommonActionHandlerDoubleTapGestureKey);
    if (!gesture) {
        // 如果没有该对象，就创建一个
        gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForDoubleTapGesture:)];
        gesture.numberOfTapsRequired = 2;
        gesture.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:gesture];
        // 绑定一下gesture
        objc_setAssociatedObject(self, &gNbCommonActionHandlerDoubleTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    // 绑定一下block
    objc_setAssociatedObject(self, &gNbCommonActionHandlerDoubleTapBlockKey, block, OBJC_ASSOCIATION_COPY);
    self.userInteractionEnabled = YES;
}

- (void)handleActionForDoubleTapGesture:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 取出上面绑定的block
        void(^action)(void) = objc_getAssociatedObject(self, &gNbCommonActionHandlerDoubleTapBlockKey);
        if (action) {
            NSDate *date = [NSDate date];
            NSTimeInterval current = [date timeIntervalSince1970] * 1000;
            NSDate *lastClickDate = objc_getAssociatedObject(self, &gNbDoubleClickClickTimeKey);
            NSTimeInterval lastClickTime = [lastClickDate timeIntervalSince1970] * 1000;
            if (current - lastClickTime > 500 || current - lastClickTime < -500) {
                action();
                objc_setAssociatedObject(self, &gNbDoubleClickClickTimeKey, date, OBJC_ASSOCIATION_COPY);
            }
        }
    }
}

- (void)addLongPressActionWithBlock:(void(^)(void))block {
    // 运行时获取长安对象
    UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &gNbCommonActionHandlerLongPressGestureKey);
    if (!gesture) {
        // 如果没有该对象，就创建一个
        gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
        [self addGestureRecognizer:gesture];
        // 绑定一下gesture
        objc_setAssociatedObject(self, &gNbCommonActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    // 绑定一下block
    objc_setAssociatedObject(self, &gNbCommonActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
    self.userInteractionEnabled = YES;
}

- (void)handleActionForLongPressGesture:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 取出上面绑定的block
        void(^action)(void) = objc_getAssociatedObject(self, &gNbCommonActionHandlerLongPressBlockKey);
        if (action) {
            NSDate *date = [NSDate date];
            NSTimeInterval current = [date timeIntervalSince1970] * 1000;
            NSDate *lastClickDate = objc_getAssociatedObject(self, &gNbLongPressClickTimeKey);
            NSTimeInterval lastClickTime = [lastClickDate timeIntervalSince1970] * 1000;
            if (current - lastClickTime > 500 || current - lastClickTime < -500) {
                action();
                objc_setAssociatedObject(self, &gNbLongPressClickTimeKey, date, OBJC_ASSOCIATION_COPY);
            }
        }
    }
}

@end
